home *** CD-ROM | disk | FTP | other *** search
/ The 640 MEG Shareware Studio 2 / The 640 Meg Shareware Studio CD-ROM Volume II (Data Express)(1993).ISO / clang / pgp20src.zip / MDFILE.C < prev    next >
C/C++ Source or Header  |  1992-07-20  |  3KB  |  104 lines

  1. /*    mdfile.c  - Message Digest routines for PGP.
  2.     PGP: Pretty Good(tm) Privacy - public key cryptography for the masses.
  3.  
  4.     (c) Copyright 1990-1992 by Philip Zimmermann.  All rights reserved.
  5.     The author assumes no liability for damages resulting from the use
  6.     of this software, even if the damage results from defects in this
  7.     software.  No warranty is expressed or implied.
  8.  
  9.     All the source code Philip Zimmermann wrote for PGP is available for
  10.     free under the "Copyleft" General Public License from the Free
  11.     Software Foundation.  A copy of that license agreement is included in
  12.     the source release package of PGP.  Code developed by others for PGP
  13.     is also freely available.  Other code that has been incorporated into
  14.     PGP from other sources was either originally published in the public
  15.     domain or was used with permission from the various authors.  See the
  16.     PGP User's Guide for more complete information about licensing,
  17.     patent restrictions on certain algorithms, trademarks, copyrights,
  18.     and export controls.  
  19. */
  20.  
  21. #include <stdio.h>
  22. #include "mpilib.h"
  23. #include "mdfile.h"
  24. #include "language.h"
  25. #include "pgp.h"
  26.  
  27. /* Begin MD5 routines */
  28.  
  29. /* Note - the routines in this module, except for MD_addbuffer,
  30.  * do not "finish" the MD5 calculation.  MD_addbuffer finishes the
  31.  * calculation in each case, usually to append the timestamp and class info.
  32.  */
  33.  
  34. /* Computes the message digest for a file from current position for
  35.    longcount bytes.
  36.    Uses the RSA Data Security Inc. MD5 Message Digest Algorithm */
  37. int MDfile0_len(MD5_CTX *mdContext, FILE *f, word32 longcount)
  38. {    int bytecount;
  39.     unsigned char buffer[1024];
  40.  
  41.     MD5Init(mdContext);
  42.     /* Process 1024 bytes at a time... */
  43.     do
  44.     {
  45.         if (longcount < (word32) 1024)
  46.             bytecount = (int)longcount;
  47.         else
  48.             bytecount = 1024;
  49.         bytecount = fread(buffer, 1, bytecount, f);
  50.         if (bytecount>0)
  51.         {    MD5Update(mdContext, buffer, bytecount);
  52.             longcount -= bytecount;
  53.         }
  54.         /* if text block was short, exit loop */
  55.     } while (bytecount==1024);
  56.     return(0);
  57. }    /* MDfile0_len */
  58.  
  59.  
  60. /* Computes the message digest for a file from current position to EOF.
  61.    Uses the RSA Data Security Inc. MD5 Message Digest Algorithm */
  62.  
  63. int MDfile0(MD5_CTX *mdContext,FILE *inFile)
  64. {    int bytes;
  65.     unsigned char buffer[1024];
  66.  
  67.     MD5Init(mdContext);
  68.     while ((bytes = fread(buffer,1,1024,inFile)) != 0)
  69.         MD5Update(mdContext,buffer,bytes);
  70.     return(0);
  71. }
  72.  
  73. /* Computes the message digest for a specified file */
  74.  
  75. int MDfile(MD5_CTX *mdContext,char *filename)
  76. {
  77.     FILE *inFile;
  78. #ifdef VMS
  79.     inFile = fopen(filename,"rb","ctx=stm");
  80. #else /* VMS */
  81.     inFile = fopen(filename,"rb");
  82. #endif /* VMS */
  83.  
  84.     if (inFile == NULL)
  85.     {    fprintf(pgpout,PSTR("\n\007Can't open file '%s'\n"),filename);
  86.         return(-1);
  87.     }
  88.     MDfile0(mdContext,inFile);
  89.     fclose (inFile);
  90.     return(0);
  91. }
  92.  
  93. /* Finish the MD5 calculation with an extra buffer implicitly appended 
  94.  * to the data.
  95.  */
  96. void MD_addbuffer (MD5_CTX *mdContext, byte *buf, int buflen, boolean finish)
  97. {
  98.     MD5Update(mdContext,buf,buflen);
  99.     if (finish)
  100.         MD5Final(mdContext);
  101. }
  102.  
  103. /* End MD5 routines */
  104.